/* 
 * Licensed Materials - Property of IBM 
 * 
 * OCO Source Materials 
 * 
 * (C) Copyright IBM Corp. 2011, 2012 All Rights Reserved 
 * 
 * The source code for this program is not published or other- 
 * wise divested of its trade secrets, irrespective of what has 
 * been deposited with the U.S. Copyright Office. 
 */

package com.ibm.ba.demo.helper;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.log4j.Logger;

/**
 * URLHelper is one utility to operate URL and attributes.
 * 
 * @author Luyi
 * 
 */
public class URLHelper {

	private static Logger logger = Logger.getLogger(URLHelper.class);

	/**
	 * Get request URL by replacing variables and adding attributes.
	 * 
	 * @param baseURL Base request URL
	 * @param variables Request variables
	 * @param attributes Request attributes
	 * @return
	 */
	public static String getRequestURL(String baseURL,
			ArrayList<NameValuePair> variables,
			ArrayList<NameValuePair> attributes) {
		String requestURL = baseURL;
		if (variables != null) {
			for (int i=0; i < variables.size(); i++) {
				NameValuePair np = variables.get(i);
				String name = np.getName();
				String value = np.getValue();
				requestURL = requestURL.replaceFirst("\\{" + name + "\\}", value);
			}
		}

		if (attributes != null) {
			for (int i=0; i < attributes.size(); i++) {
				NameValuePair np = attributes.get(i);
				String name = np.getName();
				String value = np.getValue();
				requestURL = requestURL + "?" + name + "=" + value;
			}
		}
		logger.info("requestURL is: " + requestURL);
		return requestURL;

	}
}
